package controller;


import com.sun.webkit.graphics.Ref;
import dao.DatailsDao;
import dao.RecordDao;
import entity.Datails;
import entity.Record;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import service.DatailsService;
import service.RecordService;

import util.random;

import javax.annotation.Resource;
import javax.annotation.Resources;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/j")
public class RecordCon {

            @Resource(name = "datailsDao")
            private DatailsDao datailsDao;

            @Resource(name = "datailsService")
            private DatailsService datailsService;


            @Resource(name = "recordDao")
            private RecordDao recordDao;

            @Resource(name = "recordService")
            private RecordService recordService;

            @RequestMapping("/jin")
            public String t(Model model) {
                List<Record> list = recordService.list();

                random r = new random();
                String fan = r.random(list);
                model.addAttribute("order", fan);
                return "WEB-INF/jsp/jinhuo/jinhuo";
            }

            @RequestMapping("/chu")
            public String c(Model model) {
                List<Record> list = recordService.list();

                random r = new random();
                String fan = r.random(list);
                model.addAttribute("order", fan);
                return "WEB-INF/jsp/chuhuo/chuhuo";
            }


            /*跳转到供货商统计*/
            @RequestMapping("/Qtiaozhuan")
            public String tiaozhuan(Model model) {
                return "WEB-INF/jsp/tongjibaobiao/gonghuoshangtongji";
            }


            /*按供货商名称查询*/
            @RequestMapping("/Qqueryxianshi")
            @ResponseBody
            public Map<String,Object> Qqueryxianshi(String supplierr,int pageSize,int pageNumber,String firstDate,String secondDate){
                Record record=new Record();

                int id = -1;
                if (supplierr != null && supplierr != "") {
                    id = Integer.parseInt(supplierr);
                }

                record.setSupplierid(id);

                Map<String, Object> mapResult = new HashMap<String, Object>();
                Map<String, Object> map = new HashMap<String, Object>();

                map.put("supplierid", supplierr);
                map.put("secondDate", secondDate);
                map.put("firstDate", firstDate);
                map.put("offset", (pageNumber - 1) * pageSize);
                map.put("limit", pageSize);
                List<Record> records = recordDao.QqueryGHSMC(map);

                mapResult.put("total", recordDao.QgetCount(map));
                mapResult.put("rows", records);
                return mapResult;
            }

            /*跳转到业务员统计页面*/
            @RequestMapping("/Qyewuyuan")
            public String yewuyuan(Model model) {
                return "WEB-INF/jsp/tongjibaobiao/yewuyuancaigou";
            }


            @RequestMapping("/add")
            @ResponseBody
            public int add(Record record) {
                int i = datailsService.add();
                Datails d = new Datails();
                d.setOrder(record.getOrder());
                d.setNum(record.getSupplierid());
                int j = datailsService.upd(d);
                return recordDao.add(record);
            }

            /*业务员统计*/
            @RequestMapping("/Qcxyw")
            @ResponseBody
            public Map<String, Object> Qcxyw(String agent,int pageSize, int pageNumber, String firstDate, String secondDate){
                Record record=new Record();
                int id=-1;
                if (agent != null && agent != "") {
                    id = Integer.parseInt(agent);
                }
                record.setAgent(id);


                Map<String, Object> mapResult = new HashMap<String, Object>();
                Map<String, Object> map = new HashMap<String, Object>();

                map.put("agent", agent);
                map.put("firstDate", firstDate);
                map.put("secondDate", secondDate);
                map.put("offset", (pageNumber - 1) * pageSize);
                map.put("limit", pageSize);

                List<Record> records = recordService.Qyewuyuan(map);

                mapResult.put("total",recordDao.QgetCount(map));
                mapResult.put("rows", records);

                return mapResult;

            }




            /*客户销售统计*/
            @RequestMapping("/Qkehu")
            @ResponseBody
            public Map<String,Object> Qkehu(String supplierid,int pageSize,int pageNumber,String firstDate,String secondDate){
                Record record=new Record();
                Map<String, Object> mapResult = new HashMap<String, Object>();
                Map<String, Object> map = new HashMap<String, Object>();
                int id = -1;
                if(supplierid!=null && supplierid !=""){
                    id = Integer.parseInt(supplierid);
                    map.put("supplierid",id);

                }else{
                    map.put("supplierid",supplierid);

                }
                record.setSupplierid(id);


                map.put("firstDate", firstDate);
                map.put("secondDate", secondDate);
                map.put("offset", (pageNumber-1) * pageSize);
                map.put("pageSize", pageSize);
                List<Record> records = recordService.Qkehuchuhuo(map);

                mapResult.put("total", recordDao.Qkehufenye(map));
                mapResult.put("rows", records);
                return mapResult;
    }





    /*跳转到客户销售统计*/
    @RequestMapping("/Qkehuxiaoshoutongji")
    public String Qkehuxiaoshoutongji(Model model) {
        return "WEB-INF/jsp/tongjibaobiao/kehuxiaostongji";
    }





    @RequestMapping("/query2")
    @ResponseBody
    public Map<String,Object> query(String supplierr, int pageSize, int pageNumber, String firstDate, String secondDate){
        Map<String, Object> mapResult = new HashMap<String, Object>();
        Map<String, Object> map = new HashMap<String, Object>();

        map.put("supplierid", supplierr);
        map.put("secondDate", secondDate);
        map.put("firstDate", firstDate);
        map.put("order",null);
        map.put("typeid",-1);
        map.put("offset", (pageNumber - 1) * pageSize);
        map.put("limit", pageSize);
        List<Record> records = recordService.query(map);

        mapResult.put("total", recordService.count(map));
        mapResult.put("rows", records);
        return mapResult;
    }

    @RequestMapping("/query3")
    @ResponseBody
    public Map<String,Object> query3(String supplierr, int pageSize, int pageNumber, String firstDate, String secondDate){
        Map<String, Object> mapResult = new HashMap<String, Object>();
        Map<String, Object> map = new HashMap<String, Object>();

        map.put("supplierid", supplierr);
        map.put("secondDate", secondDate);
        map.put("firstDate", firstDate);
        map.put("order",null);
        map.put("typeid",1);

        map.put("offset", (pageNumber - 1) * pageSize);
        map.put("limit", pageSize);
        List<Record> records = recordService.query(map);

        mapResult.put("total", recordService.count(map));
        mapResult.put("rows", records);
        return mapResult;
    }


    @RequestMapping("/jin2")
    public String jin2(){
        return "WEB-INF/jsp/jinhuo/WLMX";
    }
    @RequestMapping("/jin3")
    public String jin3(Model model){
        List<Record> list = recordService.list();

        random r = new random();
        String fan = r.random(list);
        model.addAttribute("order", fan);
        return "WEB-INF/jsp/jinhuo/tuihuo";
    }

}

